/***********************************************************************
 * Module:  ControlVehicleService.java
 * Author:  Administrator
 * Purpose: Defines the Class ControlVehicleService
 ***********************************************************************/

package ishitong.rvims.control.service;

import ishitong.rvims.control.persistence.ControlVehicleMapper;
import ishitong.core.common.SQLSelectWhere;
import java.util.List;
import ishitong.rvims.domain.control.ControlTask;
import ishitong.rvims.domain.control.ControlVehicle;
import org.apache.commons.lang.StringUtils;
import org.springframework.stereotype.Service;
import org.springframework.beans.factory.annotation.Autowired;

/** @pdOid ca87b44c-3417-49eb-a29b-3ed75a5a6d72 */
@Service
public class ControlVehicleService {
   /** @pdOid 5ebac4c0-1e31-4506-9998-e5b3659f50d8
       @pdRoleInfo migr=yes name=ControlVehicleMapper assc=association3 */
   @Autowired
   private ControlVehicleMapper controlVehicleMapper;
   /** @pdOid ab2dba08-951a-4116-9dc5-f343f918d81a
       @pdRoleInfo migr=yes name=ControlTaskToVehicleService assc=association10 */
   @Autowired
   private ControlTaskToVehicleService controlTaskToVehicleService;
   
   /** @param sQLSelectWhere
    * @exception Exception
    * @pdOid 2bb5447c-1cb5-4974-a4bc-5c610dfe7c37 */
   public List<ControlVehicle> findControlVehicleList(SQLSelectWhere sQLSelectWhere) throws Exception {
    return controlVehicleMapper.selectControlVehicleList(sQLSelectWhere);
   }
   
   /** @param sQLSelectWhere
    * @exception Exception
    * @pdOid 966e7e6b-5034-4f82-a52b-56fb52c2debd */
   public int getControlVehicleTotal(SQLSelectWhere sQLSelectWhere) throws Exception {
    return controlVehicleMapper.selectControlVehicleTotal(sQLSelectWhere);
   }
   
   /** @param controlVehicleId
    * @exception Exception
    * @pdOid c0008cd1-ac57-4dd7-8362-29145e525402 */
   public ControlVehicle getControlVehicleById(String controlVehicleId) throws Exception {
      return controlVehicleMapper.selectControlVehicleById(controlVehicleId);
   }
   
   /** @param controlVehicle
    * @exception Exception
    * @pdOid 9606b2aa-8649-4209-82d8-1a4775586486 */
   public void saveControlVehicle(ControlVehicle controlVehicle) throws Exception {
   
    List<ControlTask> listControlTask = controlVehicle.getListControlTask();
    String[]  controlVehicleIds = new String[1];
    String[] controlTaskIds= new String[listControlTask.size()];
    if (StringUtils.isBlank(controlVehicle.getId())) {
     controlVehicleMapper.insertControlVehicle(controlVehicle);
      } else {
    	  controlVehicleMapper.updateControlVehicle(controlVehicle);
    	  controlTaskToVehicleService.deleteByControlVehicleId(controlVehicle.getId());  
      }
    boolean bool = false;
    controlVehicleIds[0] = controlVehicle.getId();
    for(int index = 0; index<controlTaskIds.length ; index++){
     controlTaskIds[index] =listControlTask.get(index).getId();
     bool = true;
    }
    if (bool) {
     controlTaskToVehicleService.updateControlTaskToVehicle(controlVehicleIds,controlTaskIds);
    }
   }
   
   /** @param controlVehicleIds
    * @exception Exception
    * @pdOid 45b44bca-b187-452c-9da0-ec5db7c219d6 */
   public void deleteControlVehicleByIds(String controlVehicleIds) throws Exception {
    controlVehicleMapper.deleteControlVehicleByIds(controlVehicleIds);
   }

}